/**
 * 演示程序当前的 “注册/登录” 等操作，是基于 “本地存储” 完成的
 * 当您要参考这个演示程序进行相关 app 的开发时，
 * 请注意将相关方法调整成 “基于服务端Service” 的实现。
 **/
(function($, owner) {
	/**
	 * 用户登录
	 **/
	owner.login = function(loginInfo, callback) {
		callback = callback || $.noop;
		loginInfo = loginInfo || {};
		loginInfo.account = loginInfo.account || '';
		loginInfo.password = loginInfo.password || '';
		loginInfo.token = '';
		/*if (loginInfo.account.length < 5) {
			return callback('账号最短为 1个字符');
		}
		if (loginInfo.password.length < 6) {
			return callback('密码最短为 1 个字符');
		}*/
		console.log(JSON.stringify(loginInfo));
		mui.post('http://hs.tunnel.javano.cn/login', JSON.stringify(loginInfo), function(data) {
			if(data != null) {
				console.info(data.role + "--" + data.token);
				return owner.createState(data, callback);
			} else {
				return callback('用户名或密码错误');
			}
			return;
		}, 'json');

	};

	owner.createState = function(data, callback) {
		var state = owner.getState();
		state.account = data.account; //账号
		state.token = data.imtoken; //im账户
		console.log(state.token);
		state.role = data.role; //角色
		owner.setState(state);
		return callback();
	};

	/**
	 * 新用户注册
	 **/
	owner.reg = function(code, regInfo, callback) {
		callback = callback || $.noop;
		regInfo = regInfo || {};
		regInfo.account = regInfo.account || '';
		regInfo.password = regInfo.password || '';
		regInfo.token = '';
		if(regInfo.account.length < 1) {
			return callback('用户名最短需要 5 个字符');
		}
		if(regInfo.password.length < 1) {
			return callback('密码最短需要 6 个字符');
		}
		/*	if (!checkEmail(regInfo.email)) {
				return callback('邮箱地址不合法');
			}*/

		mui.post("http://hs.tunnel.javano.cn/reg/" + code, JSON.stringify(regInfo), function(data) {
			if(data == "success") {
				return callback();
			} else if(data == "ERROR1") {
				return callback("用户名已存在");
			} else if(data == "ERROR2") {
				return callback("验证码错误");
			}
		}, 'text');

	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
		console.info("账号：" + state.account + "状态：" + state.role);
	};

	var checkEmail = function(email) {
		email = email || '';
		return(email.length > 3 && email.indexOf('@') > -1);
	};

	/**
	 * 找回密码
	 **/
	owner.forgetPassword = function(code, account, callback) {
		mui.get('http://hs.tunnel.javano.cn/resetPassword', {
			code: code,
			account: account
		}, function(data) {
			mui.alert(data, '提示', '关闭', function(e) {
				e.index
			}, 'div')
		}, 'text');
		callback = callback || $.noop;
		return callback(null, '密码已重置为nuaa，请及时修改');
	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}
}(mui, window.app = {}));